
//破解，源码出售1906507927 
"ui";


ui.layout(
    <vertical padding="16">
        <text textSize="10sp" textColor="black" text="定制版 autojs源码提取,加密打包,代码解密,还原.九黎QQ1906507927" />
        {/* <input id="cardNo" text="" /> */}
        <text textSize="16sp" textColor="black" text="请输入美国号" />
        <input id="phoneNo" text="" />
        <text textSize="16sp" textColor="black" text="请输入接码地址" />
        <input id="codeUrl" text="" />
        <text textSize="16sp" textColor="black" text="请输入修改密码" />
        <input id="password" text="" />

        <button id="ok" text="确定" />

        <text marginTop="50px" text="日志" />
        <com.stardust.autojs.core.console.ConsoleView id="console" h="*" />
    </vertical>
);

ui.console.setConsole(runtime.console);
// 设置控制台字体颜色
let c = new android.util.SparseArray();
let Log = android.util.Log;
c.put(Log.VERBOSE, new java.lang.Integer(colors.parseColor("#dfc0c0c0")));
c.put(Log.DEBUG, new java.lang.Integer(colors.parseColor("#cc000000")));
c.put(Log.INFO, new java.lang.Integer(colors.parseColor("#ff64dd17")));
c.put(Log.WARN, new java.lang.Integer(colors.parseColor("#ff2962ff")));
c.put(Log.ERROR, new java.lang.Integer(colors.parseColor("#ffd50000")));
c.put(Log.ASSERT, new java.lang.Integer(colors.parseColor("#ffff534e")));
ui.console.setColors(c);



var phoneNo = "";
var codeUrl = "";

var password = "";

var lastCode = "";

var cardNo = "";//激活码


var localStorage = storages.create("autoLoginDy");
loadCache();

function saveCache() {
    var cacheData = {
        phoneNo: phoneNo,
        codeUrl: codeUrl,
        password: password,
        cardNo: cardNo,
    }
    log("save cache")
    log(cacheData)
    localStorage.put("cacheData", cacheData);
}
function loadCache() {
    var defaultCacheData = {
        phoneNo: "",
        codeUrl: "",
        password:  "plm123456",
        cardNo: "",
    }
    var cacheData = localStorage.get("cacheData", defaultCacheData);
    log("load cache")
    log(cacheData)
    initData(cacheData);
}
function initData(cacheData) {
    phoneNo = cacheData.phoneNo;
    codeUrl = cacheData.codeUrl;
    password = cacheData.password;
    cardNo = cacheData.cardNo;


    ui.phoneNo.setText(phoneNo);
    ui.codeUrl.setText(codeUrl);
    ui.password.setText(password);
    //ui.cardNo.setText(cardNo);
}





//监听音量上键按下
events.onKeyDown("volume_up", function(event){
    toast("停止脚本");
    // exit();
    home();

    engines.stopAll();
});
//监听菜单键按下
events.onKeyDown("volume_down", function(event){
    toast("停止脚本");
    // exit();
    home();

    engines.stopAll();
});



//指定确定按钮点击时要执行的动作
ui.ok.click(function () {
    // localStorage.put("url",ui.url.getText().toString());
    // saveCache();
    threads.start(function () {
       

        sleep(1000)


        toastLog("开始")
        start_shanghao();
        return ;

    
      
    });

});

function start_shanghao() {


    log("请确保开启无障碍服务哦~")

    auto.waitFor()

      //启用按键监听
      events.observeKey();

    // setScreenMetrics(1080, 2340);
    //  requestScreenCapture(false);


    phoneNo = ui.phoneNo.getText().toString();
    codeUrl = ui.codeUrl.getText().toString();
    password = ui.password.getText().toString();

    saveCache()

    app.launchApp("抖音短视频")


    loginjisu();

    //changePass();

    //app.launchApp("抖音短视频")
    // sleep(5000);

    //logindy();


    toastLog("登陆成功")

}



function logindy() {

    toastLog("开始登陆抖音")
    for (var i = 0; i < 5; i++) {
        if(text("我").exists()){
            break;
        }
        sleep(1000);
    }

    
    myWaitFor(text("同意"), 2);
    if (clickUiBounds(text("同意"))) {
        sleep(1000)
    }
    click(500, 500)

    for (var i = 0; i < 10; i++) {
        if (!text("输入手机号码").exists()) {
            toastLog("返回到首页操作")
            if (text("我").exists()) {
                clickUiBounds(text("我"))
                sleep(1000);
                break;
            }
            click(device.width - 150, device.height - 50);
            sleep(100)
            back();
            sleep(1000);
            clickUiBounds(text("以后再说"))
        } else {
            break;
        }
    }




    clickUiBounds(text("我"))
    myWaitFor(text("密码登录"), 5);
    clickUiBounds(text("密码登录"))


    myWaitFor(text("输入手机号码"), 5);
    click(130, 380);
    // if (text("输入手机号码").exists()) {
    //     var a = text("输入手机号码").findOne();
    //     var bounds = a.bounds();
    //     click(130, bounds.centerY());
    // }
    sleep(2000);
    toastLog("选择国家")
    var a = text("搜索国家和地区").findOne();
    var bound = a.bounds();
    text("搜索国家和地区").setText("美国");
    sleep(500)
    click(device.width - 50, bound.centerY())
    sleep(500);
    clickUiBounds(text("+1"));
    sleep(1000);

    toastLog("输入手机号码和密码")
    setText(0, phoneNo);
    setText(1, password);
    sleep(1000);
    if(!clickUiBounds(id("cmc"))){
        click(130,690);
    }
    sleep(1000);
    click(device.width / 2, 800)


    sleep(5000);
    if(text("+1").exists()){
        if(!clickUiBounds(id("cmc"))){
            click(130,690);
        }
        sleep(1000);
        click(device.width / 2, 800)
    }



}

function changePass() {

    toastLog("开始修改密码")
    back();
    sleep(1000);
    back();
    for (var i = 0; i < 10; i++) {
        if (text("我").exists()) {
            break;
        }
        sleep(1000);
        back();
    }
    clickUiBounds(text("我"));
    myWaitFor(text("获赞"));
    // var a = text("分享主页").findOne()
    // var bounds = a.bounds();
    // click(bounds.centerX() + 200, bounds.centerY());
    sleep(1000)
    // click(650,100);
    swipe(device.width - 100, 450, 100, 450, 500);
    sleep(1000)

    myWaitFor(text("设置"));
    clickUiBounds(text("设置"));
    sleep(1000)

    // myWaitFor(text("账号管理"));
    // clickUiBounds(text("账号管理"));
    // myWaitFor(text("更改密码"));
    // clickUiBounds(text("更改密码"));]
    toastLog("等待账号与安全出现。。。")
    myWaitFor(text("帐号与安全"));
    toastLog("等待2222")
    clickUiBounds(text("帐号与安全"));
    sleep(1000)
    myWaitFor(text("抖音密码"));
    clickUiBounds(text("抖音密码"));
    sleep(1000)


    myWaitFor(text("设置新的登录密码"));
    setText(password);
    sleep(1000)
    // toastLog(id("w7").exists())
    // clickUiBounds(id("w7"))
    click(380, 850)
//1234
    for (var i = 0; i < 50; i++) {
        var code = getCode("密码");
        if (code && code != lastCode) {
            lastCode = code;
            toastLog("尝试输入密码")
            setText(code);
            sleep(1000);
            // clickUiBounds(id("w7"))
            click(360, 860)
            sleep(3000);
            break;
        }
        sleep(3000);
    }

}


function loginjisu() {
    toastLog("好的。。。")
    myWaitForTwo(text("我知道了"), text("好的"));
    sleep(1000)
    if (text("我知道了").exists()) {
        clickUiBounds(text("我知道了"));
    }
    if (text("好的").exists()) {
        clickUiBounds(text("好的"));
    }
    // 

    toastLog("点击好的")

    toastLog("等待修改登陆地区")
    myWaitForTwo(text("38.00"), text("我"), 5);
    sleep(1000)
    if (text("38.00").exists()) {

        // var a = text("38.00").findOne();
        // var bounds = a.bounds();
        // click(bounds.centerX(), bounds.centerY() + 300);

        click(300,800)
    }

    toastLog("如果在首页")
    if (text("我").exists()) {
        clickUiBounds(text("我"))

    }


    myWaitFor(text("请输入手机号"), 5);

    for (var i = 0; i < 10; i++) {
        if (!text("请输入手机号").exists()) {
            if (text("38.00").exists()) {
                // var a = text("38.00").findOne();
                // var bounds = a.bounds();
                // click(bounds.centerX(), bounds.centerY() + 300);
                click(300,800)
            }
            toastLog("如果没有38金币，则返回到首页操作")
            if (text("我").exists()) {
                clickUiBounds(text("我"))
                sleep(1000);
                break;
            }
            click(device.width - 150, device.height - 50);
            sleep(100)
            back();
            sleep(1000);
        } else {
            break;
        }
    }

    if (text("请输入手机号").exists()) {
        var a = text("请输入手机号").findOne();
        var bounds = a.bounds();
        click(100, bounds.centerY());
        sleep(1000);
    }
    if(text("密码登录").exists()){
        clickUiBounds(100,400)
        sleep(1000);
    }
    for (var i = 0; i < 100; i++) {
        swipe(device.width / 2, device.height - 100, device.width / 2, 300, 200);
        sleep(2000);
        if (text("美国").exists()) {
            sleep(100);
            clickUiBounds(text("美国"));
            break;
        }
    }
    sleep(500);
    text("请输入手机号").setText(phoneNo);

    clickUiBounds(text("获取短信验证码"));

    var dianjiyanzhengma = text("点击选中两个相同形状的物体");
    myWaitForTwo(dianjiyanzhengma, desc("刷新"), 3);//desc =  刷新
    for (var i = 0; i < 30; i++) {
        toastLog("等待输入验证....")
        if (!dianjiyanzhengma.exists() && text("请输入验证码").exists()) {
            
            break;
        }
        sleep(2000);
    }



    for (var i = 0; i < 50; i++) {
        //1234
       // clickUiBounds(text("获取验证码"))
       toastLog("已经进入验证码界面")
        sleep(3000)
        //点击收不到短信
        if (text("收不到短信？获取语音验证码").exists()) {
            var a = text("收不到短信？获取语音验证码").findOnce();
            if (a) {
                var b = a.bounds();
                if (b) {
                    log(b)
                    click(b.centerX()+50, b.centerY());
                    //这里点击语音验证码,向中心点右边便宜50
                    return true;
                }
            }
    
        }

        var code = getCode("登录");
        if (code && code != lastCode) {
            lastCode = code;
            toast("尝试输入密码")
            //code
            text("请输入验证码").id("eks").setText(code);
            sleep(100);
            var a = text("我已阅读并同意 用户协议 和 隐私政策").findOne();
            var bounds = a.bounds();
            click(bounds.centerX() - 50, bounds.centerY());
            sleep(1000);
            clickUiBounds(text("登录"))
            break;
        }
        sleep(3000);
    }

    toastLog("登录成功！！！");
}



function getCode(containsStr) {
    //这里写接码
    log(codeUrl)
    var r = "";
    try {
        r = http.get(codeUrl);
    }
    catch (err) {
        toastLog(err);
        return "";
    }

   //var str = {};
    var str=r.body.string();
   //toastLog(str);

//直接获取网页字符串作为返回,你说是直接返回验证码
 var result = str
        toastLog(result);
        return result;

}











/**
 *  不能点击的控件，通过坐标点击
 * @param {*} uiSelector
 */
function clickUiBounds(ui) {
    if (ui.exists()) {
        var a = ui.findOne();
        if (a) {
            var b = a.bounds();
            if (b) {
                click(b.centerX(), b.centerY());
                return true;
            }
        }

    }
    return false;
}

function clickUi(ui) {
    if (ui.exists()) {
        ui.findOne().click() //推送弹框
        return true;
    }
    return false;
}

function getRandom(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}



function exitApp() {
    for (var i = 0; i < 5; i++) {
        back()
        sleep(200)
    }
}

function checkAppOpened(pname) {
    if (!packageName(pname).exists()) {
        app.launchPackage(pname);
        sleep(5000);
    }
}

function myWaitFor(ui, times) {
    times = times | 10
    for (var i = 0; i < times; i++) {
        if (ui.exists()) {
            sleep(200);
            return true;
        }
        sleep(1000);
    }
    return false;
}

function myWaitForTwo(ui, ui2, times) {
    if (!times) {
        times = 10
    }
    for (var i = 0; i < times; i++) {
        if (ui.exists() || ui2.exists()) {
            sleep(200);
            return true;
        }
        sleep(1000);
    }
    return false;
}


function commonWatchVideo(times) {
    times = times | 15;
    for (var i = 0; i < times; i++) {
        log("观看视频" + i)
        sleep(2000);
    }
    back()
    sleep(1000)
}


function skipOpenAppAd(times) {
    imes = times | 10;
    for (var i = 0; i < times; i++) {
        if (clickUiBounds(text("跳过")) || clickUiBounds(desc("跳过"))) {
            break;
        }
        sleep(1000);
    }
}

function captureAndSave(path) {
    if (!path) {
        path = "/sdcard/screenshot.png";
    }
    //请求截图
    // requestScreenCapture(false);
    //截图
    var im = captureScreen();
    // var path = "/sdcard/screenshot.png";
    //保存图片
    im.saveTo(path);
    //把图片加入相册
    media.scanFile(path);
    return im;
}


function simulateSysCapture(path) {
    if (!path) {
        path = path = "/storage/emulated/0/Pictures/Screenshots/Screenshot_20200113-123456.png";
    }
    if (files.exists(path)) {
        files.removeDir(path)
        media.scanFile(path);
    }
    sleep(1000)
    captureAndSave(path)
}



